package cn.flying.system.web.cm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.base.common.vo.Rt;
import cn.flying.base.common.vo.XmSelect;
import cn.flying.system.domain.cm.CmPosition;
import cn.flying.system.service.cm.CmPositionService;

/**
 * @description: 岗位请求处理
 * @author: lvyong
 * @date: 2022年06月05日 20:21
 * @version: 1.0
 */
@Controller
@RequestMapping("position")
public class CmPositionController {

    @Autowired
    private CmPositionService cmPositionService;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("list")
    public String list() {
        return "sys/position/list";
    }

    /**
     * 加载列表数据
     *
     * @return
     */
    @RequestMapping(value = "getPositionPageList", method = RequestMethod.POST)
    @ResponseBody
    public PageResponse<CmPosition> getPositionPageList(PageRequest<CmPosition> pageRequest) {
        PageResponse<CmPosition> pageResponse = cmPositionService.getPositionPageList(pageRequest);
        return pageResponse;
    }

    /**
     * 获取岗位下拉框数据 xm-select
     *
     * @param
     * @return
     */
    @RequestMapping(value = "getPositionSelectList", method = RequestMethod.POST)
    @ResponseBody
    public Rt<List<XmSelect>> getPositionSelectList() {
        List<XmSelect> list = cmPositionService.getPositionList();
        return Rt.success(list);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/turn/edit")
    public String turnEdit(Long id, Model model) {
        if (null != id) {
            CmPosition cmPosition = cmPositionService.getPositionById(id);
            model.addAttribute("position", cmPosition);
        } else {
            model.addAttribute("position", new CmPosition());
        }
        return "sys/position/edit";
    }

    /**
     * 保存
     *
     * @param cmPosition
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Rt save(@RequestBody CmPosition cmPosition) {
        if (cmPosition.getId() == null) {
            cmPositionService.insertPosition(cmPosition);
        } else {
            cmPositionService.updatePositionById(cmPosition);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "del/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Rt del(@PathVariable("id") Long id) {
        cmPositionService.deletePositionById(id);
        return Rt.success("删除成功！");
    }

}

